package bradd.Blackjack;




import java.lang.reflect.Array;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;



public class Table extends Activity implements OnClickListener {
	
	Deck deal;
	ArrayList<Card> m_dealer;
	ArrayList<Card> m_player;
	
	ImageView[] playerimg;
	ImageView[] dealerimg;
	ImageView[] hitimg;
	ImageView[] dealhitimg;
	int[] m_cardIds;
	ArrayList<Player> m_players;
	

@Override	
public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table);

        getWindow().setBackgroundDrawableResource(R.drawable.felt);
        

        Button button2 = (Button) findViewById(R.id.back);
        button2.setOnClickListener(this);
        
      Button button3 = (Button) findViewById(R.id.sum);
      button3.setOnClickListener(new View.OnClickListener() {
        	
			@Override
			public void onClick(View v) {
				sumtoast();
				
			}	
        }
        )	
        ;

        Button button4 = (Button) findViewById(R.id.hit);
        button4.setOnClickListener(new View.OnClickListener(){
        	@Override
        	public void onClick(View v){
        		hit();
        	}
        }
        )
        ;
        
        Button button5 = (Button) findViewById(R.id.stay);
        button5.setOnClickListener(new View.OnClickListener(){
        	@Override
        	public void onClick(View v){
        		stay();
        	}
        }
        )
        ;

        InitMembers();

        NewGame();
        
        
        m_cardIds = new int[52];
        m_cardIds[0] = R.drawable.card_1;
        m_cardIds[1] = R.drawable.card_2;
        m_cardIds[2] = R.drawable.card_3;
        m_cardIds[3] = R.drawable.card_4;
        m_cardIds[4] = R.drawable.card_49;
        m_cardIds[5] = R.drawable.card_50;
        m_cardIds[6] = R.drawable.card_51;
        m_cardIds[7] = R.drawable.card_52;
        m_cardIds[8] = R.drawable.card_45;
        m_cardIds[9] = R.drawable.card_46;
        m_cardIds[10] = R.drawable.card_47;
        m_cardIds[11] = R.drawable.card_48;
        m_cardIds[12] = R.drawable.card_41;
        m_cardIds[13] = R.drawable.card_42;
        m_cardIds[14] = R.drawable.card_43;
        m_cardIds[15] = R.drawable.card_44;
        m_cardIds[16] = R.drawable.card_37;
        m_cardIds[17] = R.drawable.card_38;
        m_cardIds[18] = R.drawable.card_39;
        m_cardIds[19] = R.drawable.card_40;
        m_cardIds[20] = R.drawable.card_33;
        m_cardIds[21] = R.drawable.card_34;
        m_cardIds[22] = R.drawable.card_35;
        m_cardIds[23] = R.drawable.card_36;
        m_cardIds[24] = R.drawable.card_29;
        m_cardIds[25] = R.drawable.card_30;
        m_cardIds[26] = R.drawable.card_31;
        m_cardIds[27] = R.drawable.card_32;
        m_cardIds[28] = R.drawable.card_25;
        m_cardIds[25] = R.drawable.card_26;
        m_cardIds[26] = R.drawable.card_27;
        m_cardIds[27] = R.drawable.card_28;
        m_cardIds[28] = R.drawable.card_25;
        m_cardIds[29] = R.drawable.card_26;
        m_cardIds[30] = R.drawable.card_27;
        m_cardIds[31] = R.drawable.card_28;
        m_cardIds[32] = R.drawable.card_21;
        m_cardIds[33] = R.drawable.card_22;
        m_cardIds[34] = R.drawable.card_23;
        m_cardIds[35] = R.drawable.card_24;
        m_cardIds[36] = R.drawable.card_17;
        m_cardIds[37] = R.drawable.card_18;
        m_cardIds[38] = R.drawable.card_19;
        m_cardIds[39] = R.drawable.card_20;
        m_cardIds[40] = R.drawable.card_13;
        m_cardIds[41] = R.drawable.card_14;
        m_cardIds[42] = R.drawable.card_15;
        m_cardIds[43] = R.drawable.card_16;
        m_cardIds[44] = R.drawable.card_9;
        m_cardIds[45] = R.drawable.card_10;
        m_cardIds[46] = R.drawable.card_11;
        m_cardIds[47] = R.drawable.card_12;
        m_cardIds[48] = R.drawable.card_5;
        m_cardIds[49] = R.drawable.card_6;
        m_cardIds[50] = R.drawable.card_7;
        m_cardIds[51] = R.drawable.card_8;

	}

	@Override
	public void onClick(View v) {
		super.onStop();
		Intent goback = new Intent(Table.this, Blackjack.class);
		Table.this.startActivity(goback);
		
	}
	@Override
    public boolean onKeyUp(int keyCode, KeyEvent event)
   {
        RelativeLayout rl = (RelativeLayout)findViewById(R.id.table1);
        int width = rl.getWidth();
        int maxWidth = width / 5;
        
        playerimg= new ImageView[5];
        playerimg[0] = (ImageView)findViewById(R.id.playercard1);
        playerimg[1] = (ImageView)findViewById(R.id.playercard2);
//        playerimg[2] = (ImageView)findViewById(R.id.playercard3);
//        playerimg[3] = (ImageView)findViewById(R.id.playercard4);
//        playerimg[4] = (ImageView)findViewById(R.id.playercard5);
        
        
        playerimg[0].setMaxWidth(maxWidth);
        playerimg[1].setMaxWidth(maxWidth);
//        playerimg[2].setMaxWidth(maxWidth);
//        playerimg[3].setMaxWidth(maxWidth);
//        playerimg[4].setMaxWidth(maxWidth);

        
        
        dealerimg= new ImageView[6];
        dealerimg[0]= (ImageView)findViewById(R.id.dealerdown);
        dealerimg[1]= (ImageView)findViewById(R.id.dealercard1);
        dealerimg[2]= (ImageView)findViewById(R.id.dealercard0);
        dealerimg[3]= (ImageView)findViewById(R.id.dealercard2);
        dealerimg[4]= (ImageView)findViewById(R.id.dealercard3);
        dealerimg[5]= (ImageView)findViewById(R.id.dealercard4);
  
        
        dealerimg[0].setMaxWidth(maxWidth);
        dealerimg[1].setMaxWidth(maxWidth);
        dealerimg[2].setMaxWidth(maxWidth);
        dealerimg[3].setMaxWidth(maxWidth);
        dealerimg[4].setMaxWidth(maxWidth);
        dealerimg[4].setMaxWidth(maxWidth);
        dealerimg[4].setMaxWidth(maxWidth);

        
        dealerimg[0].setImageResource(R.drawable.card_b1fv);
        
        {
       for (int dsize = m_dealer.size(); dsize < 2; dsize++)
       	m_dealer.add(deal.DealCard());
        for (int psize = m_player.size(); psize < 2; psize++)
        	m_player.add(deal.DealCard());
        }
        

        for (int i = 1; i < m_dealer.size(); i++)
        	{dealerimg[i].setImageResource(m_cardIds[m_dealer.get(i).getValue() * 4 + m_dealer.get(i).getSuit()]);	}
        
        for (int i = 0; i < m_player.size(); i++)
        	{playerimg[i].setImageResource(m_cardIds[m_player.get(i).getValue() * 4 + m_player.get(i).getSuit()]);}
  
        
        checkblackjack();
        //sumtoast();
        
        return true;
        
    }
	
	
	 private void InitMembers()
	    {
	    	deal = new Deck();
	    	m_dealer = new ArrayList<Card>();
	    	m_player = new ArrayList<Card>();
	    	m_players = new ArrayList<Player>();
	    }
	
	 private void DealToPlayers()
	    {
	    	for (Player player : m_players)
	    	{
	    		Card[] hand = new Card[2];
	    		hand[0] = deal.DealCard();
	    		hand[1] = deal.DealCard();
	    		
	    		player.setHand(hand);
	    	}
	    }
	 private void NewGame()
	    {
		 	deal.Shuffle();
	    	m_players.add(new Player("Player", 1500));
	    	DealToPlayers();
	    }
	private int getsum()
	{

    
	int sum = 0;
	for(int i=0; i < m_player.size(); i++) 
		{sum += (m_player.get(i).getScore());}
		
		
	return sum;
	 
	}
	private int getdealersum()
	{

    
	int sum = 0;
	for(int i=0; i < m_dealer.size(); i++) 
		{sum += (m_dealer.get(i).getScore());}
		
		
	return sum;
	 
	}
	private void sumtoast()
	{
		 Toast t = Toast.makeText(this, String.format("%d", getsum() ), Toast.LENGTH_SHORT);
         t.show();

	}
	private void blackjack()
	{
		Toast m = Toast.makeText(this,"Blackjack!",Toast.LENGTH_SHORT);
		m.show();
	}
	private void checkblackjack()
	{
		
		if (getsum() == 21){
			blackjack();
		}
		else sumtoast();
	}
	public void hit()
	{
		
		
		if (m_player.size() == 2)
		{
			m_player.add(deal.DealCard());
			playerimg[2] = (ImageView)findViewById(R.id.playercard3);
			playerimg[2].setImageResource(m_cardIds[m_player.get(2).getValue() * 4 + m_player.get(2).getSuit()]);
			checkscore();
			sumtoast();
		}
		else
		if (m_player.size() == 3){
			m_player.add(deal.DealCard());
			playerimg[3] = (ImageView)findViewById(R.id.playercard4);
			playerimg[3].setImageResource(m_cardIds[m_player.get(3).getValue() * 4 + m_player.get(3).getSuit()]);
			checkscore();
			sumtoast();
		}
		else
		if (m_player.size() == 4){
			m_player.add(deal.DealCard());
			playerimg[4] = (ImageView)findViewById(R.id.playercard5);
			playerimg[4].setImageResource(m_cardIds[m_player.get(4).getValue() * 4 + m_player.get(4).getSuit()]);
			checkscore();
			sumtoast();
		}
		
		
	}
	public void dealerhit()
	{
		if (m_dealer.size() == 2)
		{
			m_dealer.add(deal.DealCard());
			dealerimg[3] = (ImageView)findViewById(R.id.dealercard2);
			dealerimg[3].setImageResource(m_cardIds[m_dealer.get(2).getValue() * 4 + m_dealer.get(2).getSuit()]);
			
		}
		else
		if (m_dealer.size() == 3){
			m_dealer.add(deal.DealCard());
			dealerimg[4] = (ImageView)findViewById(R.id.dealercard3);
			dealerimg[4].setImageResource(m_cardIds[m_dealer.get(3).getValue() * 4 + m_dealer.get(3).getSuit()]);
			
		}
		else
		if (m_dealer.size() == 4){
			m_dealer.add(deal.DealCard());
			dealerimg[5] = (ImageView)findViewById(R.id.dealercard4);
			dealerimg[5].setImageResource(m_cardIds[m_dealer.get(4).getValue() * 4 + m_dealer.get(4).getSuit()]);
			
		}
		

		stay();
	}
	private void checkscore()
	{
	if (getdealersum() == 21){
	Toast l = Toast.makeText(this, "Dealer Wins!", Toast.LENGTH_SHORT);
	l.show();
	}
	else{
	int score = getsum();
	if (score == 21)
	{blackjack();}
	if (score <= 21)
	{sumtoast();}
	if (score > 21)
	{bust();}
	}
	}
	
	private void bust()
	{
	Toast b = Toast.makeText(this, "Bust!", Toast.LENGTH_SHORT);
	b.show();
	}
	public void stay()
	{
  
	dealerimg[2].setImageResource(m_cardIds[m_dealer.get(0).getValue() * 4 + m_dealer.get(0).getSuit()]);		
	int dealscore = getdealersum();
	
	
	if (dealscore <= 17)
	{dealerhit();}
	if (dealscore > 21)
	{playerwins();}
	if (dealscore <= 21 && dealscore > 17)
	{showdown();}
		
	}
	
	private void showdown()
	{
	int dealscore = getdealersum();
	int playscore = getsum();
	
	if (playscore > dealscore )
	{playerwins();}
	if (dealscore > playscore)
	{dealerwins();}
	if (dealscore == playscore)
	{push();}
	}
	private void playerwins()
	{
		Toast pw = Toast.makeText(this, "Player Wins!!", Toast.LENGTH_SHORT);
		pw.show();	
	}
	private void dealerwins()
	{
		Toast pw = Toast.makeText(this, "Dealer Wins!!", Toast.LENGTH_SHORT);
		pw.show();	
	}
	private void push()
	{
		Toast pw = Toast.makeText(this, "Push", Toast.LENGTH_SHORT);
		pw.show();	
	}
	}

